/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]

Output: Table E.8, E.9 

Tasks: Robustness to balanced ES on different periods
	
*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear

	
********************************************************************************
	// Prep Estimation //
********************************************************************************
	
	// Relabel outcomes for tables 
	lab var turnout_urne	"\multicolumn{6}{c}{Polling Place Turnout}"
	lab var turnout_pos_req "\multicolumn{6}{c}{Mail-in turnout}"
	lab var turnout_tot_req "\multicolumn{6}{c}{Total turnout}"		
	

	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen 	F`l'event = K==-`l'
		lab var F`l'event "Reassignment (#t-`l'#)"
	}	
	forvalues l = 0/7 {
		gen 	L`l'event = K==`l'
		lab var L`l'event "Reassignment (#t+`l'#)"
	}	
	order F1event, last	
	
	// compute group ids for DISTANCE increase/decrease, 0 else
	cap drop tmp*
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event, 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event, 0 else"

	
** Sample composition
	// Bsl sample 
	fre Ei if K==0 & fulltottreat100<=1 
		/*
						|      Freq.    Percent 
				---------+-----------------------
				1 LTW13 |          4       2.67 
				3 KOW14 |          6       4.00 
				5 BTW17 |         81      54.00 
				6 LTW18 |         13       8.67 
				7 EUW19 |          6       4.00 
				8 KOW20 |         40      26.67 
				Total	|		 150	 100.00
				-------------------------------
		*/
	// Bal around t in [-4,0]
	fre Ei if K==0 & smpl_bal_tp0 & fulltottreat100<=1 
	/*
				-------------------------------
						|      Freq.    Percent
				---------+---------------------
				3 KOW14 |          6       4.11
				5 BTW17 |         81      55.48
				6 LTW18 |         13       8.90
				7 EUW19 |          6       4.11
				8 KOW20 |         40      27.40
				Total   |        146     100.00
				-------------------------------
	*/	
	
	// Bal around t in [-4,+1]
	fre Ei if K==0 & smpl_bal_tp1==1 & fulltottreat100<=1 
	/*
						|      Freq.    Percent   
				---------+------------------------
				5 BTW17 |         81      81.00   
				6 LTW18 |         13      13.00   
				7 EUW19 |          6       6.00   
				Total   |        100     100.00   
				----------------------------------
	*/

	
	//Bal around t in [-2,+2]
	fre Ei if K==0 & smpl_bal_tm2==1 & fulltottreat100<=1
	/*
						|      Freq.    Percent  
				----------+----------------------
				3 KOW14 |          6       6.00  
				5 BTW17 |         81      81.00  
				6 LTW18 |         13      13.00  
				Total   |        100     100.00  
				---------------------------------
	*/
	
	// Bal around t in [-2,+1] 
	fre Ei if K==0 & smpl_bal_tpm==1 & fulltottreat100<=1
	/*
			---------------------------------
					|      Freq.    Percent  
			---------+-----------------------
			3 KOW14 |          6       5.66  
			5 BTW17 |         81      76.42  
			6 LTW18 |         13      12.26  
			7 EUW19 |          6       5.66  
			Total   |        106     100.00  
			---------------------------------
	*/
	
	// Bal around t in [-4,+2]
	fre Ei if K==0 & smpl_bal==1 & fulltottreat100<=1 
	/*
					|      Freq.    Percent  
			--------+------------------------
			5 BTW17 |         81      86.17  
			6 LTW18 |         13      13.83  
			Total	|		  94	 100.00
			---------------------------------
	*/	



********************************************************************************
//	 Balanced estimates for baseline specification //
********************************************************************************	
	
	local smplif "& fulltottreat100<=1"
	
	estimates clear	
	outreg, clear
	
	local j=0
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		
	// Basaeline (unbalanced)
	local `++j'
		reghdfe `v' F7event-L7event F1event  $ctr $wgt ///
				if fulltottreat100<=1 , 	absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_bsl	
		extract_N 100
		qui outreg,  $opt  keep(F4event-L2event)  ///
			addrow("Unbalanced sample", X \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
		
	// Balanced around -4/0	
	local `++j'
		reghdfe `v' F7event-L7event F1event  $ctr $wgt ///
				if smpl_bal_tp0==1 `smplif', 	absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tp0	
		extract_N 100
		qui outreg,  $opt  keep(F4event-L2event)  ///
			addrow("Balanced sample, $ \tau \in$", "$ [-4,0] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
		
	// Balanced around -2/+1
	local `++j'
		reghdfe `v' F7event-L7event F1event $ctr $wgt ///
					if  smpl_bal_tpm==1 `smplif', absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tpm	
		extract_N 100
		qui outreg,  $opt  keep(F4event-L2event)  ///
			addrow("Balanced sample, $ \tau \in$", "$ [-2,1] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
			
	// Balanced around -4/+1
	local `++j'
		reghdfe `v' F7event-L7event F1event $ctr $wgt ///
					if  smpl_bal_tp1==1 `smplif' , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tp1
		extract_N 100
		qui outreg,  $opt  keep(F4event-L2event)  ///
			addrow("Balanced sample, $ \tau \in$", "$ [-4,1] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')" \"", "`:var lab `v''")
	
	// Balanced around -2/+2
	local `++j'
		reghdfe `v' F7event-L7event F1event $ctr $wgt ///
					if  smpl_bal_tm2==1 `smplif', absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tm2
		extract_N 100
		qui outreg,  $opt  keep(F4event-L2event)  ///
			addrow("Balanced sample, $ \tau \in$", "$ [-2,2] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
		
	// Balanced around -4/+2
	local `++j'
		reghdfe `v' F7event-L7event F1event  $ctr $wgt ///
				if smpl_bal==1 `smplif' , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_bal
		extract_N 100
		qui outreg,  $opt  keep(F4event-L2event)  ///
			addrow("Balanced sample, $ \tau \in$", "$ [-4,2] $" \#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)')	ctitle("", "(`j')")
	}	
	
	* TABLE E8. Balanced Sample Results–Pooled Reassignments
	outreg using "$tables/Table_E8_ES_bal_samples_bsl", replay tex replace fragment
	cleantex "$tables/Table_E8_ES_bal_samples_bsl.tex"  ,  replace	
	
	

*********************************************************************************
	// Balanced estimates for heterogeneity by distance increase/ decrease //
*********************************************************************************	

	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn				// a := decrease
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b = F`l'event *ind_dist_up				// b:= increase
		lab var F`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b+F`l'event_a==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b = L`l'event *ind_dist_up	// b:= increase
		lab var L`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b+L`l'event_a==L`l'event	
	}
	// ORDER dummies
	order *event_b, last
	order F1event*,last		
	
	local smplif "& fulltottreat100<=1"
	
	estimates clear	
	outreg, clear
	
	local j=0
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		
	// Basaeline (unbalanced)
	local `++j'
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b  $ctr $wgt ///
				if fulltottreat100<=1 , 	absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_bsl	
		extract_N 100
		qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ///
			addrow(Unbalanced sample, X \ Balanced sample, \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
		
	// Balanced around -4/0	
	local `++j'
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b  $ctr $wgt ///
				if smpl_bal_tp0==1 `smplif', 	absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tp0	
		extract_N 100
		qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ///
			addrow(Balanced sample, "$ \tau \in [-4,0] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
		
	// Balanced around -2/+1
	local `++j'
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr $wgt ///
					if  smpl_bal_tpm==1 `smplif', absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tpm	
		extract_N 100
		qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ///
			addrow(Balanced sample, "$ \tau \in [-2,1] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
			
	// Balanced around -4/+1
	local `++j'
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr $wgt ///
					if  smpl_bal_tp1==1 `smplif' , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tp1
		extract_N 100
		qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ///
			addrow(Balanced sample, "$ \tau \in [-4,1] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')" \"", "`:var lab `v''")
	
	// Balanced around -2/+2
	local `++j'
		qui reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr $wgt ///
					if  smpl_bal_tm2==1 `smplif', absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_tm2	
		extract_N 100
		qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ///
			addrow(Balanced sample, "$ \tau \in [-2,2] $" \ #treated precincts, `r(N_T)' \ #control precincts, `r(N_C)') ctitle("", "(`j')")
		
		
	// Balanced around -4/+2
	local `++j'
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b  $ctr $wgt ///
				if smpl_bal==1 `smplif' , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

		estimates store `v'_bal
		extract_N 100
		qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ///
			addrow(Balanced sample, "$ \tau \in [-4,2] $" \#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)')	ctitle("", "(`j')")
	}	
	
	* TABLE E9. Balanced Sample Results–Effects by Distance
	outreg using "$tables/Table_E9_ES_bal_samples_dist2", replay tex replace fragment
	cleantex "$tables/Table_E9_ES_bal_samples_dist2.tex"  ,  replace	
	